import subprocess

from django.http import JsonResponse, HttpResponse
from django.shortcuts import render

# Create your views here.
from django.views.decorators.csrf import csrf_exempt
from django.views.decorators.http import require_POST


def home(request):
    return render(request, 'index.html')


@csrf_exempt
def compute(request):
    code = request.POST.get('code')
    try:
        code = 'print(' + code + ')'
        result = subprocess.check_output(['python', '-c', code], universal_newlines=True, stderr=subprocess.STDOUT,timeout=30)
    except:
        result='输入错误'

    return JsonResponse(data={'result': result})

